#!/usr/bin/env/python3.7
# -*- coding: utf-8 -*-

"""
@File    : app_upload_mysql.py
@Time    : 12/12/2021
@Author  : Silence
@Version : 1.0
@Contact : silence2021silence@163.com
@License : (C)Copyright 2021-2021, Silence
@Desc    : None
"""
import time
import pymysql
import socket

database = pymysql.connect(host="120.24.51.156", user="dormitory_system", password="123456",
                                   database="dormitory_system", charset="utf8")

class UploadMySQL():
    def upload_student_info(student_info_list):
        if student_info_list != None:
            t = True
            while t:
                for info in student_info_list:
                    ip_address = info['ip_address']
                    student_number = info['student_number']
                    name = info['name']
                    room = info['room']
                    grade = info['grade']
                    class_ = info['class']
                    cursor = database.cursor()
                    sql_1 = """
                            INSERT INTO `dormitory_system`.`student_info` (
	                            `id`, `ip_address`, `student_number`, `name`,
	                            `room`, `grade`, `class`
                                )
                            VALUES
	                            (
		                            'NULL', '%s', '%s', '%s', '%s', '%s', '%s'
	                            )
                    """
                    sql_2 = sql_1 % (ip_address, student_number, name, room, grade, class_)
                    database.ping(reconnect=True)
                    cursor.execute(sql_2)
                    database.commit()
                    cursor.close()
                    database.close()
                t = False
            return 'success'
